package com.lizemin.springaspect.aspect;

import cn.hutool.core.util.ArrayUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

/**
 * @author lzm
 * @date 2025/3/30 17:42
 * @description
 */
@Slf4j
@Aspect
//@Component
public class LogAspect {

    @Pointcut("execution(* com.lizemin.springaspect.controller.*.*(..))" +
            " || execution(* com.lizemin.springaspect.service.*.*(..))")
    public void logPointCut() {

    }

    /**
     * 打印日志的切面
     *
     * @param pjp 切入点
     * @return 方法的执行结果
     */
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Signature signature = pjp.getSignature();
        String methodName = signature.toLongString().split(" ")[2];
        Object[] args = pjp.getArgs();
        log.info("{}方法的请求入参为：{}", methodName, ArrayUtil.toString(args));
        Object result = pjp.proceed();
        log.info("{}方法的返回值为：{}", methodName, result);
        return result;
    }

}
